package edu.imac.angryfood.utils;

import java.util.ArrayList;

import edu.imac.angryfood.game.Angryfood;


/**
 * Local host with all the players,
 */
public class PlayersHost {
	public Angryfood game; 
	/**/
	private Account compte_courant;
	private ArrayList<Account> comptes;

	/****************** Design pattern singleton ******************/
	private PlayersHost () { }
	private static PlayersHost server = new PlayersHost();
	public static PlayersHost getInstance () { return server; }
	/****************** End design pattern ***********************/

	/**
	 * Load file define in Constant
	 */
	public void load () {
		
		IOJDom.loadFile();
		this.comptes = IOJDom.loadAccount ();


	}


	/**
	 * Return all account of the host.
	 * 
	 * 
	 * @return ArrayList of all accounts
	 */
	public ArrayList<Account> getComptes () {
		return this.comptes;
	}

	
	/**
	 * Add an account at "comptes.xml"
	 * 
	 * 
	 * @return ArrayList of all accounts
	 */
	public void addCompte (Account cpt) {
		IOJDom.addAcount(cpt);
		this.comptes.add(cpt);

	}

	/**
	 * Check if an account of the same name exist
	 * 
	 * 
	 * @return True if it's OK
	 */
	public boolean verifCompte(Account cpt){
		for (int i =0;i<this.comptes.size();i++){
			if(this.comptes.get(i).getName().equals(cpt.getName())){
				return false;
			}
		}
		return true;
	}

	/**
	 * Check if a pseudo has been enter
	 * 
	 * 
	 * @return True if it's OK
	 */
	public boolean isPseudo(String ps){
		
		if(ps != ""){
			return true;
		}
		return false;
	}

	/**
	 * Select an account to be the current one traited
	 * 
	 * 
	 */
	public void setCompte_courant(Account compte_courant) {
		game.setCurrentPlayer(compte_courant);
	}

	/**
	 * Get the current account
	 * 
	 * 
	 * @return an Account
	 */
	public Account getCompte_courant() {
		return compte_courant;
	}

	private void echanger(int a, int b)
	{
		Account temp = this.getComptes().get(a);
		this.getComptes().add(a, this.getComptes().get(b));
		this.getComptes().remove(a+1);

		this.getComptes().add(b,temp); 
		this.getComptes().remove(b+1);
	}

	/**
	 * Scores are sorted
	 * 
	 * 
	 */

	public void sortByScore(int debut, int fin){
		int gauche = debut-1;
		int droite =fin +1;

		float pivot = this.getComptes().get(debut).getMax_score();
		if(debut >= fin)
			return;

		while(true)
		{
			do droite--; while(this.getComptes().get(droite).getMax_score() < pivot);
			do gauche++; while(this.getComptes().get(gauche).getMax_score() > pivot);

			if(gauche < droite)
				echanger( gauche, droite);
			else break;
		}

		sortByScore(debut, droite);
		sortByScore(droite+1, fin);

	}


	/**
	 * Return the name of the player
	 * 
	 * @return name of the account
	 */
	public String toStringComptes(){
		String s="";
		for (int i=0;i<this.getComptes().size();i++){
			s=s+this.getComptes().get(i).getName()+ " // ";
		}
		return s;
	}



	/**
	 *Register all this.comptes in the xml file of the accounts by modifying the scores and the reached levels
	 *
	 */
	
	public void saveComptes(){
		
		for (int i = 0;i< this.getComptes().size();i++){
			Account cpt = this.getComptes().get(i);
			IOJDom.modifScoreLevel(cpt.getName(), cpt.getMax_score(), cpt.getLast_level());
		}
	}
		
	/**
	 *Delete an account
	 *
	 */
		public void deleteComptes(Account cpt){
			
			IOJDom.deleteAccount(cpt.getName());
			this.comptes.remove(cpt);
			
	}
}





